package com.company.controller;

import com.company.base.GzhModel;
import com.company.base.User;
import com.company.core.annotation.Login;
import com.company.core.result.Result;
import com.company.core.result.ResultUtil;
import com.company.core.validator.ValidatorUtils;
import com.company.core.validator.group.AddGroup;
import com.company.pushTools.WXAccessToken;
import com.company.utils.Constant;
import com.company.utils.GzhUtils;
import com.company.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: planet-company
 * @description: 系统配置
 * @author: jyz
 * @create: 2019-04-11 21:13
 **/
@Api(value = "推送")
@RestController
@RequestMapping("api/system")
public class SystemConfig {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    //服务号获取用户信息
    public final static String ZZHUSERINFOURL = "https://api.weixin.qq.com/sns/userinfo?access_token=";
    @Autowired
    private RedisUtils R;
    @Autowired
    GzhUtils gzhUtils;

    /**
     * 设置微信配置
     * @return
     */
    @Login
    @PostMapping("/wxConfig")
    @ApiOperation(value = "设置微信配置")
    public Result wxConfig(HttpServletRequest request,@ApiParam("gzhModel") @RequestBody GzhModel gzhModel){
        ValidatorUtils.validateEntity(gzhModel,AddGroup.class);
        R.set(Constant.GZH_Config+gzhModel.getAppId(),gzhModel);
        setUserAndGZH(request,gzhModel.getAppId());
        return ResultUtil.success();
    }

    /**
     * 获取当前机构的微信配置
     * @return
     */
    @Login
    @GetMapping("/getWxConfig")
    @ApiOperation(value = "获取当前机构的微信配置")
    public Result getWxConfig(HttpServletRequest request){
        String phone = (String) request.getSession().getAttribute("phone");
        User user = R.get(Constant.CompanyUser+phone,User.class);
        return ResultUtil.success(R.get(Constant.GZH_Config+user.getAppId(),GzhModel.class));
    }


    /**
     * 修改当前机构的微信配置
     * @return
     */
    @Login
    @PostMapping("/updateWxConfig")
    @ApiOperation(value = "修改当前机构的微信配置")
    public Result updateWxConfig(HttpServletRequest request,@ApiParam("gzhModel") @RequestBody GzhModel gzhModel){
        ValidatorUtils.validateEntity(gzhModel,AddGroup.class);
        R.set(Constant.GZH_Config+gzhModel.getAppId(),gzhModel);
        setUserAndGZH(request,gzhModel.getAppId());
        return ResultUtil.success();
    }


    /**
     * 获取用户的微信信息
     * @return
     */
    @PostMapping("/getWXUserInfo")
    @ApiOperation(value = "获取用户的微信信息")
    public Result getWXUserInfo(HttpServletRequest request,@ApiParam("code") @RequestParam String code){
        LOGGER.info("获取用户的微信信息 ----- >"+code);
        String phone = "15658163361";
        User user = R.get(Constant.CompanyUser+phone,User.class);
        WXAccessToken wxAccessToken = gzhUtils.getCurrentAccessTokenByCode(user.getAppId(),code);
        if (StringUtils.isNotEmpty(wxAccessToken.getAccess_token())){
            R.set(Constant.wxInfo+phone,wxAccessToken);
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }



    /**
     * 配置机构信息时，动态的把当前登录用户挂在这个机构下面
     * @param request
     * @param appId
     */
    public void setUserAndGZH(HttpServletRequest request,String appId){
        //把当前登录用户动态的挂在这个微信公众号下面
        String phone = (String) request.getSession().getAttribute("phone");
        User user = R.get(Constant.CompanyUser+phone,User.class);
        user.setAppId(appId);
        R.set(Constant.CompanyUser+phone,user);
    }
}
